home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11631 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.2 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: binary tree question
  5. Date: 25 Mar 1996 07:54:27 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4j6fjjINNc2p@keats.ugrad.cs.ubc.ca>
  8. References: <4isglj$cgg@netaxs.com> <4iumkjINNsp3@keats.ugrad.cs.ubc.ca> <4ivpff$a6j@netaxs.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4ivpff$a6j@netaxs.com>, J. A. McNamara <grulm@netaxs.com> wrote:
  12. >Kazimir Kylheku (c2a192@ugrad.cs.ubc.ca) wrote:
  13. >: This is not a pure ``inorder'' traversal. The nodes are being added to the
  14. >: linked list in order, but you are freeing in post-order (children are visited
  15. >: first, then you free the parent).
  16. >
  17. >hmmm, yeah, I see yr point.  would that affect anything?  I think I did it
  18. >that way originally because I was freeing the children, so I wanted it in
  19. >a safe place.
  20.  
  21. No. It is perfectly valid, in a binary tree traversal function, to effect
  22. three orders of traversal. It's just a matter of where you put the statements.
  23. The things you want to be done in order, you put between the recursive calls to
  24. process the left and right child. The things you want in post-order (children
  25. visited before parent), you put after these two statements. Things to be done
  26. in pre-order you put at the beginning, before processing the children. You
  27. get all three orders simultaneously.
  28.  
  29. >:  >free(tn->l); tn->l=NULL;
  30. >:  >free(tn->r); tn->r=NULL;
  31. >:  >
  32. >:  >. . .  which gave me some serious garbage, though I'm not quite sure why.
  33. >
  34. >: Because you probably did not check whether the children are null pointers or
  35. >: not, only whether tn is a null pointer. Just because tn is a valid tree node
  36. >: doesn't mean that tn->l or tn->r are.
  37. >
  38. >jesus, I'm really batting 1000 here.  I don't grok why that would give the 
  39. >list pointers to garbage, though; that stuff was postorder, just like the
  40. >free() in the whole function I posted.  ??
  41.  
  42. Because if you call free() on tn->l or tn->r, and they happen to be invalid
  43. garbage pointers, you are calling for undefined behavior. Anything could
  44. happen: malloc heap corruption, etc.
  45.  
  46. >: The above modification will also fail to
  47. >: free the root node.
  48. >
  49. >ah!  small potatoes, but worth noting.
  50.  
  51. It could lead to a memory leak if you make and destroy a lot of trees at some
  52. higher level of the code.
  53.  
  54. >: Your code appears fine. That it's not actually freeing memory is not
  55. >: surprising. Many free() implementations don't free memory, they only return
  56. >: free blocks to a list from where subsequent malloc() invocations can obtain
  57. >: memory quickly.
  58. >
  59. >how isn't that freeing?  if this'll clarify things any, I want to free the 
  60. >tree to make room for the list, so one shrinks as the other grows.  I'd
  61. >guess (!) that the malloc() in the addlink() function would then pull 
  62. >memory off that list, right?   (and both structs are the same size, three 
  63. >pointers).  does it then not free the memory for *non*-malloc() purposes?
  64. >Or only sorta free it?  This is something I'm kinda foggy about in 
  65. >general.
  66.  
  67. That is correct.  The memory that is deallocated with free() is generally not
  68. available for non-malloc purposes. It _can_ be, but it typically isn't.
  69.  
  70. In standard C, there is no way to have any other ``non malloc'' purpose anyway.
  71. Malloc is the only way to obtain pieces of dynamic memory. To ``really free''
  72. memory would mean that when you call free(), memory is actually returned to the
  73. operating system so that it can be used for other programs. On systems with a
  74. global heap or with virtual memory, this is possible.
  75.  
  76. Even on virtual memory systems like UNIX, though, free() does not generally do
  77. this.
  78.  
  79. Repeated use of malloc() and free() can fragment your memory, meaning that gaps
  80. have been created  between allocated segments that are too small to be useful
  81. and can't be coalesced.
  82.  
  83. Have a look at K&R2, section 8.7 Example---A Storage Allocator.  It shows a
  84. simple C implementation of malloc() and free(). N.B.: you cannot write your own
  85. function called malloc() as the book suggests, for it is a reserved symbol
  86. according to ANSI. The book is wrong in this respect.
  87.  
  88.  
  89.  
  90.  
  91. >
  92. >-- 
  93. >                          j a mcnamara  aramancm a j
  94. >                      grulm@netaxs.com  moc.sxaten@mlurg
  95.  
  96.  
  97. -- 
  98.  
  99.